package de.tabuma.projectview.according;

import java.util.*;
import java.util.logging.*;
import javax.servlet.http.*;

import de.tabuma.projectview.*;

import de.tabuma.pm.externalcontent.entity.ExternalContent;
import de.tabuma.pm.externalcontent.logik.ExternalContentFacade;

import de.tabuma.pm.externalproject.entity.*;
import de.tabuma.pm.externalproject.logik.*;

import de.tabuma.pm.design.*;
import de.tabuma.pm.project.entity.*;

import de.tabuma.pm.projectnavigation.entity.ProjectNavigation;
import de.tabuma.pm.projectnavigation.logik.ProjectNavigationFacade;

import de.tabuma.ssoclient.entity.Attribute;
import java.util.regex.Pattern;

/**
 * @author $Author$
 * @version $Revision$
 * @since $Date$
 */
public class AccodingFacade {

    private static final Logger log = Logger.getLogger(AccodingFacade.class.getName());

    public static String getForm(ExternalProject ep, List<Attribute> aList, HttpServletRequest request) {
        String str = "";
        str += "\t\t\t<form name=\""+constants.projectForm + ep.getId()+"\" action=\""+ep.getLoginUrl()+"\" method=\"" + (ep.getPost()==Boolean.TRUE ? "POST" : "GET") + "\" target=\""+constants.contentIframe1+"\">\n";
        str += "\t\t\t\t<div>\n";
        List<LoginUserParameter> list = LoginUserParameterFacade.getList(ep.getId().intValue());
        List<HiddenParameter> list1 = HiddenParameterFacade.getList(ep.getId().intValue());
        for(int i=0;i<list.size();i++) {
            LoginUserParameter lup = list.get(i);
            String value = "";
            String name = ep.getName()+"/"+lup.getNameForUser()+" ("+lup.getId().intValue()+")";
            for(int j=0;j<aList.size();j++) {
                Attribute attribute = aList.get(j);
                if(attribute.getName().equals(name)) {
                    j = aList.size();
                    value = attribute.getValue();
                }
            }
            str += "\t\t\t\t<input type=\"hidden\" name=\"" + lup.getName() + "\" value=\""+value+"\">\n";
        }
        for(int i=0;i<list1.size();i++) {
            HiddenParameter hp = list1.get(i);
            str += "\t\t\t\t<input type=\"hidden\" name=\"" + hp.getName() + "\" value=\"" + hp.getValue() + "\">\n";
        }
        str += "\t\t\t\t</div>\n";
        str += "\t\t\t</form>\n";
        return str;
    }

    public static String getForm(String link, int id) {
        String str = "";
        java.util.regex.Pattern pattern = Pattern.compile("&");
        link = link.replaceAll(pattern.pattern(), "&amp;");
        str += "\t\t\t<form name=\""+constants.linkForm + id+"\" action=\""+link+"\" method=\"GET\" target=\""+constants.contentIframe1+"\"></form>\n";
        return str;
    }

    public static String getProjectString(HttpServletRequest request, Project project, List<Attribute> aList, String ssoId, Design design) {
        int projectId = project.getId().intValue();
        String waitLoginMessage = "System will login into System. You have to wait 5 Seconds to get the right url.";
        String str = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n";
        str += "<html>\n";
        str += "\t<head>\n";
        str += "\t\t<title>"+project.getName()+"</title>\n";
        str += "\t\t<script type=\"text/javascript\" src=\"scripts/jquery-1.3.2.min.js\"></script>\n";
        str += "\t\t<script type=\"text/javascript\" src=\"scripts/menu.js\"></script>\n";
        str += "\t\t<script type=\"text/javascript\" src=\"javascriptview?"+constants.projectId+"="+projectId+"&amp;"+constants.ssoId+"="+ssoId+"\"></script>\n";
        if(design.getCssUrl().equals("")) {
            str += "\t\t<link rel=\"stylesheet\" href=\"css/according.css\" type=\"text/css\">\n";
        }
        else {
            str += "\t\t<link rel=\"stylesheet\" href=\""+design.getCssUrl()+"\" type=\"text/css\">\n";
        }
        str += "\t</head>\n";
        str += "\t<body onload=\"window.setTimeout('init()', 2000);\">\n";
        str += "\t<div class=\"textbox\">\n";
        str += "\t\t<div id=\"div_"+constants.contentIframe+"\">\n";
        str += "\t\t\t<div id=\""+constants.contentFrame+"\" class=\"contentFrame\">\n";
        str += "\t\t\t\t<div id=\""+constants.contentFramea+"\" class=\"contentFrame1\">\n";
        str += "\t\t\t\t\t<div id=\""+constants.contentIframe+"\" class=\"iframe\">\n";
        str += "\t\t\t\t\t</div>\n";
        str += "\t\t\t\t</div>\n";
        str += "\t\t\t</div>\n";
        str += "\t\t</div>\n";
        str += "\t\t<div id=\"div_"+constants.contentIframe1+"\">\n";
        str += "\t\t\t<div id=\""+constants.contentFrame1+"\" class=\"contentFrame\">\n";
        str += "\t\t\t\t<div id=\""+constants.contentFrame1a+"\" class=\"contentFrame1\">\n";
        str += "\t\t\t\t\t<iframe name=\""+constants.contentIframe1+"\" class=\"iframe\">\n";
        str += "\t\t\t\t\t</iframe>\n";
        str += "\t\t\t\t</div>\n";
        str += "\t\t\t</div>\n";
        str += "\t\t</div>\n";
        str += "\t</div>\n";
        str += "\t<div id=\""+constants.breadcrumpsElement+"\"></div>\n";
        str += "\t<div id=\""+constants.loginWaitWindow+"\">"+waitLoginMessage+"</div>\n";
        str += "\t<div class=\"logo\"></div>\n";
        str += AccordingLinkStructurFacade.getLinkStructur(projectId, ssoId, "setting", "Design", "current", design.getId().intValue());

        List<ExternalProject> epList = ExternalProjectFacade.getList(projectId);
        for(int i=0;i<epList.size();i++) {
            ExternalProject ep = epList.get(i);
            str += "\t\t<div class=\"form\">\n";
            str += getForm(ep, aList, request);
            str += "\t\t</div>\n";
        }

        List<ProjectNavigation> pnList = ProjectNavigationFacade.getList(projectId);
        for(int i=0;i<pnList.size();i++) {
            ProjectNavigation pnw = pnList.get(i);
            if(pnw.getNavMode()==de.tabuma.pm.constants.ProjectNavigationExternalContent) {
                ExternalContent ec = ExternalContentFacade.getWithNavigationId(pnw.getId().intValue());
                str += "\t\t<div class=\"form\">\n";
                str += getForm(ec.getUrl(), ec.getNavigationId());
                str += "\t\t</div>\n";
            }
        }

        str += "\t</body>\n";
        str += "</html>\n";
        return str;
    }
}